package com.gold.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gold.entity.Exchange;
import com.gold.entity.Expense;
import com.gold.entity.Game;
import com.gold.entity.Mobile;
import com.gold.entity.User;
import com.gold.mapper.ExchangeMapper;
import com.gold.mapper.ExpenseMapper;
import com.gold.mapper.GameMapper;
import com.gold.mapper.MobileMapper;
import com.gold.mapper.UserMapper;
import com.gold.myexception.Myexception;
import com.gold.service.ExpenseService;

@Service
@Transactional
public class ExpenseServiceImpl implements ExpenseService {

	@Autowired
	private ExpenseMapper em;
	@Autowired
	private GameMapper gm;
	@Autowired
	private UserMapper um;
	@Autowired
	private ExchangeMapper exm;
	@Autowired
	private MobileMapper mm;

	public void insertExpense(Expense expense) {
		em.insertExpense(expense);
	}

	public List<Expense> selectExpenseByUserId(int userId) {
		return em.selectExpenseByUserId(userId);
	}

	public void updateExpense(Expense expense) {
		em.updateExpense(expense);
	}

	public void buyGame(Map<String, String> map) throws Myexception {
		int userId = Integer.parseInt(map.get("userId"));
		int gameId = Integer.parseInt(map.get("gameId"));
		Game game = gm.selectGameByGameId(gameId);
		User user = um.selectUserByUserId(userId);
		if (game != null && user != null) {
			boolean flag = true;
			int expOprationsId = Integer.parseInt(map.get("bugGameStatusId"));// 支付方式
			List<Expense> expenseList = em.selectExpenseByUserId(userId);
			if (expenseList.size() > 0) {// 判断有没有购买过---》购买过游戏
				for (int i = 0; i < expenseList.size(); i++) {
					if (expenseList.get(i).getExpGameId() == gameId) {
						long nowTime = System.currentTimeMillis();
						String updateTimeString = expenseList.get(i).getExpUpdateTime();
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						long updateTime = 0;
						try {
							updateTime = sdf.parse(updateTimeString).getTime();
						} catch (ParseException e) {
							e.printStackTrace();
						}
						if (nowTime > updateTime && (nowTime - updateTime) < 86400000) {
							if (expenseList.get(i).getExpDownloads() < 3) {// 不需要付钱
								flag = !flag;
								// 更新账单
								Expense expense = new Expense(expenseList.get(i).getExpId(), userId, gameId, null, 0, expOprationsId, null, null, null, expenseList.get(i).getExpDownloads() + 1);
								em.updateExpense(expense);
							}
						}
					}
				}
			}
			if (flag) {
				if (expOprationsId == 1) {// 话费支付
					if (game.getGameTariffe() > user.getUserTariffe()) {
						throw new Myexception("话费余额不足");
					}
					// 添加账单
					Expense expense = new Expense(0, userId, gameId, null, game.getGameTariffe(), expOprationsId, null, null, null, 0);
					em.insertExpense(expense);
					// 修改用户余额
					Map<String, String> tmpMap = new HashMap<String, String>();
					tmpMap.put("userTariffe", (user.getUserTariffe() - game.getGameTariffe()) + "");
					tmpMap.put("userId", userId + "");
					um.updateUserTariffeByUserId(tmpMap);
					// 根据用户手机区域赠送游币
					int MobileNumber = Integer.parseInt(user.getUserPhone().substring(0, 2));
					Mobile mobile = mm.selectMobileByMobileNumber(MobileNumber);
					if (mobile != null) {
						int mobileAreaId = mobile.getMobileAreaId();
						List<Exchange> exchangeList = exm.selectExchangeByProvId(mobileAreaId);
						if (exchangeList.size() > 0) {
							int statusId = exchangeList.get(0).getExchangeStatusId();
							if (statusId == 2) {
								tmpMap.put("userCurrency", (user.getUserCurrency() + game.getGameTariffe()) + "");
								um.updateUserCurrencyByUserId(tmpMap);
							} else {
								int charge = exchangeList.get(0).getExchangeCharge();
								tmpMap.put("userCurrency", (user.getUserCurrency() + game.getGameTariffe() * charge) + "");
								um.updateUserCurrencyByUserId(tmpMap);
							}
						} else {
							tmpMap.put("userCurrency", (user.getUserCurrency() + game.getGameTariffe()) + "");
							um.updateUserCurrencyByUserId(tmpMap);
						}
					} else {
						tmpMap.put("userCurrency", (user.getUserCurrency() + game.getGameTariffe()) + "");
						um.updateUserCurrencyByUserId(tmpMap);
					}
				} else if (expOprationsId == 2) {// 游币支付
					if (game.getGameCurrency() > user.getUserCardAmount()) {
						if (game.getGameCurrency() > (user.getUserCurrency() + user.getUserCardAmount())) {
							throw new Myexception("游币和密保余额不足");
						} else {
							// 添加账单
							Expense expense = new Expense(0, userId, gameId, null, game.getGameCurrency(), expOprationsId, null, null, null, 0);
							em.insertExpense(expense);
							// 修改用户余额
							Map<String, String> tmpMap = new HashMap<String, String>();
							tmpMap.put("userId", userId + "");
							tmpMap.put("userCurrency", (user.getUserCurrency() - (game.getGameCurrency() - user.getUserCardAmount())) + "");
							tmpMap.put("userCardAmount", 0 + "");
							um.updateUserCardAmountByUserId(tmpMap);
							um.updateUserCurrencyByUserId(tmpMap);
						}
					} else {
						// 添加账单
						Expense expense = new Expense(0, userId, gameId, null, game.getGameCurrency(), expOprationsId, null, null, null, 0);
						em.insertExpense(expense);
						// 修改用户余额
						Map<String, String> tmpMap = new HashMap<String, String>();
						tmpMap.put("userId", userId + "");
						tmpMap.put("userCardAmount", (user.getUserCardAmount() - game.getGameCurrency()) + "");
						um.updateUserCardAmountByUserId(tmpMap);
					}
				} else {
					throw new Myexception("不支持该类型支付");
				}
			}
			// 修改游戏下载次数
			Map<String, String> tmpMap = new HashMap<String, String>();
			tmpMap.put("gameId", gameId + "");
			tmpMap.put("gameDown", game.getGameDown() + 1 + "");
			gm.updateGameDownByGameId(tmpMap);
		} else {
			throw new Myexception("未知错误");
		}
	}
}
